//
//  PostCell.swift
//  WeiboDemo
//
//  Created by 姬向阳 on 2020/4/18.
//  Copyright © 2020 SUNNYJ. All rights reserved.
//

import SwiftUI

struct PostCell: View {
    let post : Post
    
    @EnvironmentObject var userData : UserData
    
    // 是否显示评论输入页
    @State var presentComment : Bool = false
    
    var bindingPost : Post {
        userData.post(forId: post.id)!
    }
    
    
    var body: some View {
        var post =  bindingPost
        return VStack(alignment: .leading ,spacing: 10) {
            HStack(spacing: 5) {
                // 头像
                post.avatarImage
                    .resizable()
                    .scaledToFill()
                    .frame(width: 50, height: 50)
                    .clipShape(Circle())
                    .overlay(
                        PostVIPBadge(vip: post.vip)
                            .offset(x: 16 ,y: 16)
                )
                VStack(alignment: .leading, spacing: 5) {
                    // 用户昵称
                    Text(post.name)
                        .font(.system(size: 16))
                        .foregroundColor(Color(red: 242/255, green: 99/255, blue: 4/255))
                        .lineLimit(1)
                    // 日期
                    Text(post.date)
                        .font(.system(size: 11))
                        .foregroundColor(.gray)
                }
                .padding(.leading,10)
                
                Spacer()
                
                // 关注按钮
                if !post.isFollowed {
                    Button(action: {
                        post.isFollowed = true
                        self.userData.update(post)
                    }) {
                        Text("关注")
                            .font(.system(size: 14))
                            .foregroundColor(.orange)
                            .frame(width: 50, height: 26)
                            .overlay(
                                RoundedRectangle(cornerRadius: 13)
                                    .stroke(Color.orange, lineWidth: 1)
                        )
                    }
                    .buttonStyle(BorderlessButtonStyle())
                }
            }
            // 文字内容
            Text(post.text)
                .font(.system(size: 17))
            // 图片内容
            if !post.images .isEmpty {
                PostImageCell(images: post.images, width: UIScreen.main.bounds.width - 30 )
            }
            Divider()
            HStack {
                Spacer()
                // 评论按钮
                PostCellToolBarButton(image: "message", text: post.commentCountText, color: .black) {
                    // 点击将评论输入页显示设置为true
                    self.presentComment = true
                }
                .buttonStyle(BorderlessButtonStyle())
                // 根据presentComment决定是否推出评论输入页
                .sheet(isPresented: $presentComment) {
                    /*
                    模态推出评论输入页,
                    模态推出的View不属于之前View的子View,
                    之前的View的层级关系不包括模态推出的View,
                    模态推出的View相当于创建了一个新的View的层级关系,
                    因此,在模态推出View时,要用到环境对象的话,要在模态推出时添加环境对象
                    */
                    CommentInputView(post: post).environmentObject(self.userData)
                }
                Spacer()
                // 点赞按钮
                PostCellToolBarButton(image: post.isLiked ? "heart.fill" : "heart",
                                      text: post.likeCountText,
                                      color: post.isLiked ? .red : .black) {
                    if post.isLiked {
                        post.isLiked = false
                        post.likeCount -= 1
                    }else{
                        post.isLiked = true
                        post.likeCount += 1
                    }
                    self.userData.update(post)
                }
                .buttonStyle(BorderlessButtonStyle())
                Spacer()
            }
            Rectangle()
                .padding(.horizontal , -15)
                .frame(height: 10)
                .foregroundColor(Color(red: 238/255, green: 238/255, blue: 238/255))
        }
        .padding(.horizontal, 15)
        .padding(.top , 15)
    }
}

struct PostCell_Previews: PreviewProvider {
    static var previews: some View {
        PostCell(post: UserData().recommendPostList.list[0]).environmentObject(UserData())
    }
}
